package ru.dc.gui.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FavouriteHubEditDlg extends JDialog {
    private JPanel contentPane;
    private JButton okBtn;
    private JButton cancelBtn;
    private JCheckBox autoconnect;
    private JTextField addr;
    private JTextField nick;
    private JComboBox encoding;
    private JTextField nameField;
    private int result = JOptionPane.CANCEL_OPTION;

    public FavouriteHubEditDlg(JFrame owner) {
        super(owner);
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(okBtn);
        setSize(400, 300);
        setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() -getWidth())/2,
                (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() -getHeight())/3);

        okBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        cancelBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

// call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

// call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        result = JOptionPane.OK_OPTION;
        dispose();
    }

    private void onCancel() {
        result = JOptionPane.CANCEL_OPTION;
        dispose();
    }

    public void setName(String name) {
        nameField.setText(name);
    }

    public String getName() {
        return nameField.getText();
    }

    public void setAddress(String name) {
        addr.setText(name);
    }

    public String getAddress() {
        return addr.getText();
    }

    public void setNick(String name) {
        nick.setText(name);
    }

    public String getNick() {
        return nick.getText();
    }

    public void setEncoding(String name) {
        encoding.setSelectedItem(name);
    }

    public String getEncoding() {
        return (String) encoding.getSelectedItem();
    }

    public boolean isAutoconnect() {
        return autoconnect.isSelected();
    }

    public void setAutoconnect(boolean autoconnect) {
        this.autoconnect.setSelected(autoconnect);
    }

    public int execute() {
        setVisible(true);
        return result;
    }

}
